(function($, window, document, undefined) {
    //插件初始化
    function init(target, options) {
        var settings = $.extend({}, $.fn.socialShare.defaults, options);
		//初始化各个组件
        // var $social_group = "<div class='social_group'>"
		// + "<a target='_blank' class='msb_network_button weixin'>weixin</a>"
		// + "<a target='_blank' class='msb_network_button qZone'>qZone</a>"
		// + "</div>";
        // $(target).append($social_group);
        // $(target).addClass("socialShare");

		//添加QQ空间分享事件
		$(document).on("click",".msb_network_button.qZone",function(){
			qZone(this,settings);
		});
		//添加微信分享事件
		$(document).on("click",".msb_network_button.weixin",function(){
			weixinShare(this,settings);
		});
		$(document).on("click",".share",function(){
			$('.shareLinks').show();
			$('.container').addClass('mask')
		});
		$(document).on("click",".shareBottom",function(){
			$('.shareLinks').hide();
			$('.container').removeClass('mask');
		});
    }

	function replaceAPI (api,options) {
		api = api.replace('{url}', options.url);
		api = api.replace('{title}', options.title);
		api = api.replace('{content}', options.content);
		api = api.replace('{pic}', options.pic);

		return api;
	}

	function qZone(target,options){
		var options = $.extend({}, $.fn.socialShare.defaults, options);
		window.open(replaceAPI(qzone,options));
	}

	function weixinShare(target,options){
		window.open(replaceAPI(weixin,$.extend({},$.fn.socialShare.defaults,options)));
	}

    $.fn.socialShare = function(options, param) {
        if(typeof options == 'string'){
		    var method = $.fn.socialShare.methods[options];
			if(method)
				return method(this,param);
		}else
			init(this,options);
    }


    //插件默认参数
    $.fn.socialShare.defaults = {
        url: "http://www.baidu.com",
        title: "商超世界爆款产品",
        content: '商超世界爆品来袭,速来抢购',
        pic: ''
    }

	//插件方法
	$.fn.socialShare.methods = {
		//初始化方法
		init:function(jq,options){
			return jq.each(function(){
				init(this,options);
			});
		},
		qZone:function(jq,options){
			return jq.each(function(){
				qZone(this,options);
			})
		},
		weixinShare:function(jq,options){
		    return jq.each(function(){
				weixinShare(this,options);
			});
		}
	}
	//分享地址
	var qzone = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={url}&title={title}&pics={pic}&summary={content}';
	var weixin = 'http://qr.liantu.com/api.php?text={url}';
	var qq = "http://connect.qq.com/widget/shareqq/index.html?url={{url}}&title={title}&pics={pic}&summary={content}"
})(jQuery, window, document);
